package com.kingyea.mobilepolice.apimanage.provider.service.impl;

import com.kingyea.mobilepolice.apimanage.provider.bean.Provider;
import com.kingyea.mobilepolice.apimanage.provider.dao.ProviderMapper;
import com.kingyea.mobilepolice.apimanage.provider.service.ProviderService;
import com.kingyea.mobilepolice.apimanage.systemsync.Send2Queue;
import com.kingyea.mobilepolice.apimanage.utils.StateCode;
import com.kingyea.mobilepolice.apimanage.utils.page.PageInfo;
import com.kingyea.mobilepolice.system.exception.BusinessException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author Mr.Lin
 * @version v1.0.0
 * @description Provider 实现类
 * @date Created in 2018-02-27 10:56:01
 */
@Service
@Transactional
public class ProviderServiceImpl implements ProviderService {

    private static final Logger LOGGER = LoggerFactory.getLogger(ProviderServiceImpl.class);

    @Resource
    private ProviderMapper providerMapper;

    /**
     * 通过ID查找对象
     *
     * @param id
     * @return
     */
    @Override
    public Provider getById(Integer id) throws Exception {
        return this.providerMapper.getById(id);
    }

    /**
     * 通过ID查找对象
     *
     * @param serialNo
     * @return
     */
    @Override
    public Provider getBySerialNo(String serialNo) throws Exception {
        return this.providerMapper.getBySerialNo(serialNo);
    }

    /**
     * 列表
     *
     * @param
     * @return
     */
    @Override
    public List<Provider> list(Provider provider) throws Exception {
        List<Provider> list = providerMapper.list(provider);
        return list;
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Send2Queue
    @Override
    public int deleteById(Integer id) throws Exception {
        providerMapper.deleteById(id);
        return 1;
    }

    /**
     * 软删除
     *
     * @param id
     * @return
     */
    @Send2Queue
    @Override
    public int changeDelFlagById(Integer id) throws Exception {
        providerMapper.changeDelFlagById(id);
        return 1;
    }

    /**
     * 保存
     *
     * @param provider
     * @return
     */
    @Send2Queue
    @Override
    public Provider save(Provider provider) throws Exception {
        if (StringUtils.isEmpty(provider.getSerialNo()) || StringUtils.isEmpty(provider.getProvider())
                || StringUtils.isEmpty(provider.getContacts()) || StringUtils.isEmpty(provider.getContactsTel())) {
            throw new BusinessException(StateCode.NO_ARGUMENT_ERROR);
        }
        //  唯一标识重复
        Provider isExist = providerMapper.getBySerialNoAndNotDel(provider.getSerialNo());
        if (isExist != null) {
            throw new BusinessException(StateCode.UNIQUE_IDENTIFIER_EXIST);
        }
        provider.setCreateTime(new Date());
        providerMapper.save(provider);
        return provider;
    }

    /**
     * 更新
     *
     * @param provider
     * @return
     */
    @Send2Queue
    @Override
    public Provider update(Provider provider) throws Exception {
        try {
            providerMapper.update(provider);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return provider;
    }

    @Override
    public Object searchByCondition(Map<String, Object> map) throws Exception {
        PageInfo.startPage(map);
        return PageInfo.getPageInfo(providerMapper.searchByCondition(map));
    }

    /**
     * 带条件统计服务方数量
     */
    @Override
    public Integer countByCondition(Map<String, Object> map) {
        return providerMapper.countByCondition(map);
    }


}
